<?php

namespace zhtfrontend\controllers;

use common\components\helpers\Fun;
use yii\data\Pagination;
use yii\helpers\ArrayHelper;
use yii\web\BadRequestHttpException;
use zhtfrontend\models\datas\Mechanic;
use zhtfrontend\models\datas\MechanicSubsidy;

class MechanicsubsidyController extends \zhtfrontend\components\bases\BaseController
{
    public function actionIndex()
    {
       $param = \Yii::$app->request->get();

       $model = MechanicSubsidy::find()
           ->select(['dmd_mechanic_subsidy.*',' SUM(dmd_mechanic_subsidy.amount) as total','dmd_mechanic.name as mechanic_name'])
           ->joinWith('mechanic');

        if (!empty($param['start_time']) && !empty($param['end_time'])) {
            $start_time = strtotime(trim($param['start_time']));
            $end_time = strtotime(trim($param['end_time']).' 23:59:59');
        }else{
            $start_time =  strtotime(date('Y-m-01',strtotime('-1 month')));
            $end_time = strtotime(date('Y-m-t 23:59:59', strtotime('-1 month')));
        }
        $model->where(['between','dmd_mechanic_subsidy.class_date',$start_time,$end_time]);
        if(!empty($param['mechanic_id'])){
            $model->andWhere(['dmd_mechanic_subsidy.mechanic_id'=>$param['mechanic_id']]);
        }
        $model->groupBy('dmd_mechanic_subsidy.mechanic_id');
        $pages = new Pagination(['totalCount' => $model->count(), 'pageSize' => 12]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $data = $model->offset($pages->offset)->limit($pages->limit)->asArray()->all();
        foreach ($data as $key=>$item){
            $is_settlement = 0;
            $unsettled = MechanicSubsidy::find()->where(['status'=>0,'mechanic_id'=>$item['mechanic_id']]);
            $settled = MechanicSubsidy::find()->where(['status'=>1,'mechanic_id'=>$item['mechanic_id']]);
            $isSettlement = MechanicSubsidy::find()->where(['status'=>0,'mechanic_id'=>$item['mechanic_id']]);
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $start_time = strtotime(trim($param['start_time']));
                $end_time = strtotime(trim($param['end_time']).' 23:59:59');
            }else{
                $start_time =  strtotime(date('Y-m-01',strtotime('-1 month')));
                $end_time = strtotime(date('Y-m-t 23:59:59', strtotime('-1 month')));
            }
            $unsettled->andWhere(['between','class_date',$start_time,$end_time]);
            $settled->andWhere(['between','class_date',$start_time,$end_time]);
            $rowSettlement = $isSettlement->andWhere(['between','class_date',$start_time,$end_time])->one();
            $data[$key]['unsettled'] = $unsettled->sum('amount');
            $data[$key]['settled'] = $settled->sum('amount');
            if($rowSettlement !== null){
                $is_settlement = 1;
            }
            $data[$key]['is_settlement'] = $is_settlement;
        }
        $mechanic_ids = MechanicSubsidy::find()->select('mechanic_id')->where(['between','class_date',$start_time,$end_time])->groupBy('mechanic_id')->column();
        $mechanicList = Mechanic::find()->where(['id'=>$mechanic_ids])->all();
        $mechanicLists['null']= '请选择';
        foreach ($mechanicList as $item){
            $mechanicLists[$item->id] = $item->name;
        }
//        $mechanicLists = ArrayHelper::map($mechanicList,'id','name');
        $start = !empty($param['start_time'])?$param['start_time']:date('Y-m-d',$start_time);
        $end = !empty($param['end_time'])?$param['end_time']:date('Y-m-d',$end_time);
        return $this->render('index',['data'=>$data,'pages'=>$pages,'mechanicList'=>$mechanicLists,'start'=>$start,'end'=>$end]);
    }

    public function actionDetails(){
        $param = \Yii::$app->request->get();
        if(empty($param['id'])){
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        $model = MechanicSubsidy::find()->where(['mechanic_id'=>$param['id']]);
        if (!empty($param['start']) && !empty($param['end'])) {
            $start_time = strtotime(trim($param['start']));
            $end_time = strtotime(trim($param['end']).' 23:59:59');
            $model->andWhere(['between','class_date',$start_time,$end_time]);
        }
        if(!empty($param['mechanic_id'])){
            $model->andWhere(['mechanic_id'=>$param['mechanic_id']]);
        }
        if(isset($param['status']) && $param['status'] != null){
            $model->andWhere(['status'=>$param['status']]);
        }
        $pages = new Pagination(['totalCount' => $model->count(), 'pageSize' => 12]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $data = $model->offset($pages->offset)->limit($pages->limit)->all();
        $start = !empty($param['start'])?$param['start']:null;
        $end = !empty($param['end'])?$param['end']:null;
        $data = Fun::asArray($data,['mechanic_name','goods_name','status_name']);

        $mechanicModel = MechanicSubsidy::find()->select('mechanic_id');
        if (!empty($param['start']) && !empty($param['end'])) {
            $start_time = strtotime(trim($param['start']));
            $end_time = strtotime(trim($param['end']).' 23:59:59');
            $mechanicModel->where(['between','class_date',$start_time,$end_time]);
        }
        $mechanic_ids = $mechanicModel->groupBy('mechanic_id')->column();
        $mechanicList = Mechanic::find()->where(['id'=>$mechanic_ids])->all();
        $mechanicList = ArrayHelper::map($mechanicList,'id','name');

        return $this->render('details',['data'=>$data,'pages'=>$pages,'mechanicList'=>$mechanicList,'start'=>$start,'end'=>$end]);


    }

}
